<?php
/**
 * Extractors Mapping Grid
 * 
 * @author Xavier VILAIN
 * @package Orx\Etl
 * @license #license-orx#
 * 
 */
use Fox\Html\Input;
use Orx\Etl\DataType;
use Orx\Etl\Extractor;
use Orx\Etl\Extractor\Reference;
use Fox\Html\Block;
use Fox\Generator;
use Fox\Behaviors;
use Fox\Html\Button;
$xview = Xview::instance();
$extractorid = $xview->val('extractor',false);

$grid = new Fox\Grid('mapping',Generator::url('extractors','mapping',null,
				array('extractor'=> $extractorid)));

$grid->fieldcat(array(
				array('datasource_position','position'),
				array('datasource_fieldname','source_fieldname'),
				array('etl_tg_sequence','sequence'),
				array('etl_tg_reference','reference'),
				array('etl_tg_ref_attribute','attribute'),
				array('etl_datatype','datatype'),
				array('elmnm','dataelement'),
				array('is_mandatory','mandatory'),
				array('etl_conversion_rule','conversion_rule')
				));		
$grid->setInput(true);

// Position
$grid->fieldcat()->field('position');

$sequences = array(1 =>1,2=>2,3=>3);
// Sequence (Max 10)
$grid->fieldcat()->field('sequence')
	->headerAlign('center')
	->align('center')
	->input()
	->inputAsDropList(false,false,true)
	->inputRenderer()
	->options($sequences);

// Target Oject References
$attributes = array();
$references = Fox\Dao::finder(Reference::className())->find();
foreach($references as $reference) {
	$attributes[$reference->reference] = $reference->attributes();
}
$grid->fieldcat()->field('reference')
	->headerAlign('center')
	->align('center')
	->input()
	->inputAsDropList(true,false,true)
	->inputRenderer()
	->options($references,'reference','objname');
	
// Target Object Attribute
$grid->fieldcat()->field('attribute')
	->headerAlign('center')
	->align('center')
	->input()
	->inputAsDropList(true,false,true)
	->linkFrom($grid->fieldcat()->field('reference'),true)
	->source(Generator::url('extractors','reference','attributes'))
	->inputRenderer()
	->optionsGenerator(Generator::useFunction(function ($data) use ($attributes){
		
		if(!empty($data['reference']) && $data['reference'] > 0) {
			return $attributes[$data['reference']];
		} else {
			return 1;
		}
	}));
// Data Types
$datatypes = Fox\Dao::finder(DataType::className())->find();
$grid->fieldcat()->field('datatype')
	->headerAlign('center')
	->align('center')
	->input()
	->inputAsDropList(true,false,false)
	->inputRenderer()
	->options($datatypes,'datatype',Generator::call('label'));
// Data Element
$grid->fieldcat()->field('dataelement')
	->size(Input::SIZE_MEDIUM);
// Conversion Rule
$grid->fieldcat()->field('conversion_rule')
	->input(false)
	->SetHiddenInput(true)
	
	->setGenerator(Generator::useFunction(function($data) use($extractorid) {
		if(!empty($data['conversion_rule'])) {
			return array('fldvl' => 1,'srcnm' => 'iedit','ilink' =>'?' );
		} else {
			return array('fldvl' => 0,'srcnm' => 'add','ilink' => '?');
		}
	}))
	->renderer()->behaviors(
		Behaviors::action(Generator::url('extractors','mapping','rule',
						array('extractor' => $extractorid,
								'position' => Generator::useElement('position')
							)),
							true
		)
	);

$grid->addEmptyLine();		
// Grid Action
/*
$grid->footer()->add(new Button('add'))->behaviors(
	Behaviors::action(Generator::url('extractors','mapping','add'))
);
*/
// Initialize Items by importing a file
$grid->footer()->add(new Button('import'))->behaviors(
	Behaviors::action(Generator::url('extractors','mapping','import',array('extractor' => $extractorid)),true)
);

if($xview->ajax()) {
	try {
		$data = array();
		$extractor = Fox\Dao::load(Extractor::className(),$extractorid);
		foreach($extractor->items() as $item) {
			$data[] = $item->getData();
		}
		$grid->initialize($data,true);
	} catch(Fox\Object\Exception\NotExist $e) {
		
	}
	echo $grid->display();
} else {
	$block = new Block('bMapping',Msg(Orx\Etl::BUNDLE,Orx\Etl::MAPPING_BETWEEN_SOURCE_AND_TARGET));
	$block->add($grid);
	return $block;
}